﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Helper.Controls
{
    public partial class TimePicker : UserControl
    {
        public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register("SelectedTime", typeof(TimeSpan),
            typeof(TimePicker), new PropertyMetadata(new TimeSpan(0, 0, 0),TimeChanged));

        public TimePicker()
        {
            InitializeComponent();
        }
        private static void  TimeChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            TimePicker t = d as TimePicker;
            if ((e.NewValue==null)||(e.NewValue==DependencyProperty.UnsetValue))
            {
                t.HrTxt.Text = "00";
                t.MinTxt.Text = "00";
                t.SecTxt.Text = "00";
                return;
            }
            t.HrTxt.Text = ((TimeSpan)e.NewValue).Hours.ToString("D2");
            t.MinTxt.Text = ((TimeSpan)e.NewValue).Minutes.ToString("D2");
            t.SecTxt.Text = ((TimeSpan)e.NewValue).Seconds.ToString("D2");
        }
        
        public TimeSpan SelectedTime
        {
            get { return (TimeSpan)this.GetValue(SelectedTimeProperty); }
            set
            {                
                this.SetValue(SelectedTimeProperty,value);
            }
        }

        private void HrBtn_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedTime = new TimeSpan(1, 0, 0).Add(this.SelectedTime);
        }

        private void MinBtn_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedTime = new TimeSpan(0, 1, 0).Add(this.SelectedTime);
        }

        private void SecBtn_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedTime = new TimeSpan(0, 0, 1).Add(this.SelectedTime);
        }

        
    }
}
